from .models import DW


class Department:
    idn = None
    name = None
    cc = None

    def __init__(self, idn):
        self.idn = idn
        self.cc = int(DW.objects.get(bm=self.idn).cc)

    def getroot(self):
        if self.cc ==1:
            self.name = DW.objects.get(bm=self.idn).name
            return self
        root_id = int(str(self.idn)[:3] + "000")
        root = Department(root_id)
        root.name = root.getname()
        return root

    def getname(self):
        if self.name is None:
            self.name = DW.objects.get(bm=self.idn).name
        return self.name
